<template>
    <van-sticky z-index="100">
        <van-nav-bar title="确认订单" left-text="返回" left-arrow @click-left="onBack" />
    </van-sticky>

    <van-contact-card type="add" @click="toAddress" v-if="JSON.stringify(addressInfo) === '{}'" />
    <van-contact-card type="edit" :tel="addressInfo.mobile" :name="addressInfo.consignee" @click="toAddress" v-else />

    <van-card
        v-for="item in CartList"
        :key="item.id"
        :num="item.nums"
        :price="item.total"
        :title="item.product.name"
        :thumb="item.product.thumb_cdn"
    >
        <template #desc>
            ￥ {{ item.price }}
        </template>
    </van-card>

    <van-cell-group inset>
        <van-field
            v-model="content"
            rows="5"
            autosize
            label="订单备注"
            type="textarea"
            maxlength="100"
            placeholder="请输入订单备注"
            show-word-limit
            colon
            label-width="4.5em"
        />
    </van-cell-group>

    <van-submit-bar :price="total" button-text="提交订单" @submit="onSubmit" />

</template>

<script setup>
import { getCurrentInstance, ref, reactive, onMounted, computed } from 'vue';

const { proxy } = getCurrentInstance();
const proid = ref(proxy.$route.query.proid ? proxy.$route.query.proid : 0);
const action = ref(proxy.$route.query.action ? proxy.$route.query.action : '');
const cartids = ref(proxy.$route.query.cartids ? proxy.$route.query.cartids : 0);
const business = reactive(proxy.$cookies.get('business') ? proxy.$cookies.get('business') : {});
const addressInfo = ref(proxy.$cookies.get('address') ? proxy.$cookies.get('address') : {});
const CartList = ref([]);
const CartData = ref({});
const content = ref();

onMounted(() => {
    getCartData();
    content.value = '';
    if(JSON.stringify(addressInfo.value) === '{}')
    {
        getAddressDefault();
    }
});

const onSubmit = () => {

    proxy.$showConfirmDialog({
        title:'下单',
        message:'是否确认购买商品？'
    }).then(async (res) => {
        
        let data = {
            busid:business.id,
            cartids:cartids.value===0?CartData.value.id:cartids.value,
            addrid:addressInfo.value.id,
            content:content.value
        }
        console.log(data);
        // console.log(content);
        let result = await proxy.$api.OrderConfirm(data);

        if(result.code === 1)
        {
            proxy.$showNotify({
                type:'success',
                message:result.msg,
                duration:1500,
                onClose:() => {
                    proxy.$cookies.remove('address');
                    proxy.$router.push('/product/order/index');
                }
            });

            return;
        }else{
            proxy.$showNotify({
                type:'warning',
                message:result.msg,
                duration:1500
            });

            return;
        }
    }).catch((err) => {
        
    });

    
}

const total = computed(() => {
    let total = 0;

    if(CartList.value.length > 0)
    {
        for(let item of CartList.value)
        {
            total += parseFloat(item.total);
        }
    }

    return total * 100;
});

const CartAdd = async () => {

    let data = {
        busid:business.id,
        proid:proid.value,
        action:action.value
    }

    let result = await proxy.$api.CartAdd(data);
    // console.log(result.data);
    if(result.code === 1)
    {
        
        CartData.value = result.data;
        // console.log(CartData.value);
        return;
    }else{
        proxy.$showNotify({
            type:'warning',
            message:result.msg,
            duration:1500
        });

        return;
    }
}

const getCartData = async () => {
    if(action.value === "buy"){
        // console.log(22222);
        await CartAdd();
    }
    // console.log(action.value);
    let data = {
        busid:business.id,
        cartids:cartids.value===0?CartData.value.id:cartids.value
    }
    // console.log(CartData.value.id);
    // console.log(1111);
    // console.log(cartids.value);
    let result = await proxy.$api.CartInfo(data);

    if(result.code === 1)
    {
        CartList.value = result.data;
        return;
    }else{
        proxy.$showNotify({
            type:'warning',
            message:result.msg,
            duration:1500,
            onClose:() => {
                proxy.$router.back();
            }
        });

        return;
    }
}

const getAddressDefault  = async () => {
    let data = {
        busid:business.id
    }

    let result = await proxy.$api.AddressDefault(data);

    if(result.code === 1)
    {
        addressInfo.value = result.data;
    }
}

const toAddress = () => {
    proxy.$router.push({path:'/business/address/index',query:{action:'order'}});
}

const onBack = async () => {

    if(action.value === "buy"){
        let data = {
            busid:business.id,
            id:CartData.value.id
        }
        let result = await proxy.$api.CartDel(data);
    }
    proxy.$cookies.remove('address');
    proxy.$router.back();
}
</script>